package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.From;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.dao.ICommentDao;
import com.magicode.cms.dao.IDictionaryDao;
import com.magicode.cms.entiy.base.Dictionary;
import com.magicode.cms.entiy.base.Program;
import com.magicode.cms.service.IDictionary;
import com.magicode.common.dao.ICommonDaoService;
@Service("servicedictionary")
public class DictionaryServiceImpl extends BaseServiceImpl<Dictionary, IDictionaryDao> implements IDictionary{
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
			
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			querywhere.put("p", "p");
			String hql = this.getHql(querywhere);
			hql += "  order by "+entity_alias_name+".orderCode asc ";
			hql += "  ,"+entity_alias_name+".title desc "; 
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("code", ""+entity_alias_name+".code as code ");
			gridsel.put("dicType", ""+entity_alias_name+".dicType.title as dicType ");
			gridsel.put("type", ""+entity_alias_name+".type as type ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			gridsel.put("valid", "case when "+entity_alias_name+".valid = 1 then '有效' else '无效' end "
					+ " as valid ");
			gridsel.put("fatherType", "concat(pt.title,p.title) as fatherType ");
			return this.queryMapDataList(hql, gridsel, start, size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	}
	public List<Map<String,Object>> select(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere);
			hql += "  order by "+entity_alias_name+".orderCode asc "; 
			hql += "  ,"+entity_alias_name+".title desc "; 
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("code", ""+entity_alias_name+".code as code ");
			gridsel.put("remark", ""+entity_alias_name+".remark as remark ");
			gridsel.put("dicType", ""+entity_alias_name+".dicType.title as dicType ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			gridsel.put("valid", "case when "+entity_alias_name+".valid = 1 then '有效' else '无效' end "
					+ " as valid ");
			gridsel.put("parentId", ""+entity_alias_name+".parent.id as parentId ");
			gridsel.put("keywords", ""+entity_alias_name+".keywords as keywords ");
			return this.queryMapDataList(hql, gridsel, 0, 0);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String hql ="from " +Dictionary.class.getName()+" "+entity_alias_name+" ";
		if(null!=querywhere && !querywhere.isEmpty()){
			if(querywhere.containsKey("p")){
				hql+=" left join "+entity_alias_name+".parent as p ";
				hql+=" left join "+entity_alias_name+".parent.dicType as pt ";
			}
			hql+=" where 1=1 ";
		if(querywhere.containsKey(field_id)){
			hql+=" and "+ entity_alias_name+"."+field_id+" = '"+querywhere.get(field_id)+"' ";
		}
		if(querywhere.containsKey("dicTypeId")){
			hql+=" and "+ entity_alias_name+".dicType.id = '"+querywhere.get("dicTypeId")+"' ";
		}
		if(querywhere.containsKey("dicTypeCode")){
			hql+=" and "+ entity_alias_name+".dicType.code = '"+querywhere.get("dicTypeCode")+"' ";
		}
		//dicType
		if(querywhere.containsKey("dicType")){
			hql+=" and "+ entity_alias_name+".dicType.code = '"+querywhere.get("dicType")+"' ";
		}
		//dicType
		if(querywhere.containsKey("key")){
			hql+=" and ( "
					+ " "+ entity_alias_name+".code like '%"+querywhere.get("key")+"%' or"
					+ " "+ entity_alias_name+".title like '%"+querywhere.get("key")+"%' "
					+ " ) ";
		}
		if(querywhere.containsKey("type")){
			hql+=" and "+ entity_alias_name+".dicType.code = '"+querywhere.get("type")+"' and "+ entity_alias_name+".orderCode <> '060' ";
		}
		if(querywhere.containsKey("other")){
			hql+=" and ("+ entity_alias_name+".type != 'other' or "+ entity_alias_name+".type = null) ";
		}
		if(querywhere.containsKey("offlinetype")){
			hql+=" and  "+ entity_alias_name+".type ='"+querywhere.get("offlinetype")+"' ";
		}
		if(querywhere.containsKey("code")){
			hql+=" and "+ entity_alias_name+".code like '%"+querywhere.get("code")+"%' ";
		}
		if(querywhere.containsKey("title")){
			hql+=" and "+ entity_alias_name+".title like '%"+querywhere.get("title")+"%' ";
		}
		if(querywhere.containsKey("valid")){
			hql+=" and "+ entity_alias_name+".valid = '"+querywhere.get("valid")+"' ";
		}
		if(querywhere.containsKey("parentId")){
			hql+=" and "+ entity_alias_name+".parent.id = '"+querywhere.get("parentId")+"' ";
		}
		if(querywhere.containsKey("pardicTypeId")){
			hql+=" and "+ entity_alias_name+".parent.dicType.id = '"+querywhere.get("pardicTypeId")+"' ";
		}
		if(querywhere.containsKey("parentno")){
			hql+=" and ("+ entity_alias_name+".parent =null or "+ entity_alias_name+".parent ='') ";
		}
		if(querywhere.containsKey("parentIsNull")){
			hql+=" and "+ entity_alias_name+".parent is null ";
		}
		
		if(querywhere.containsKey("NetPlatform")){
			String title = querywhere.get("NetPlatform");
			hql = hql+" and ( "+entity_alias_name+".dicType.code='NetPlatform' "
					+ " and "+entity_alias_name+".title like '%"+title+"%' ) ";
		}
		}
		return hql;
	}

	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "名称");
		map.put("width", 150);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "code");
		map.put(json_title, "编码");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "dicType");
		map.put(json_title, "类型");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "other");
		map.put(json_title, "类别（其他）");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "orderCode");
		map.put(json_title, "排序码");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "valid");
		map.put(json_title, "有效性");
		map.put("width", 50);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "fatherType");
		map.put(json_title, "父级数据");
		map.put("width", 120);
		beaninfo.add(map);
		return beaninfo;
	}	
	
	@Override
	public IDictionaryDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return dictionarydao;
	}
	@Autowired
	@Qualifier("daodictionary")
	private IDictionaryDao dictionarydao;
	public IDictionaryDao getDictionarydao() {
		return dictionarydao;
	}

	public void setDictionarydao(IDictionaryDao dictionarydao) {
		this.dictionarydao = dictionarydao;
	}
	@Autowired
	private ICommonDaoService ICommonDaoServiceImpl;
	
	@Override
	/**
	 * @pram typecode 类型编码
	 * @pram code 编 码
	 */
	public Dictionary getDictionaryByDictionaryTypeCode(String typecode,String code)
			throws Exception {
		Map<String,String> prams = new HashMap<String,String>();
		prams.put("dicType", typecode);
		prams.put("code", code);
		List<Dictionary> dics = dictionarydao.queryDataList(this.getHql(prams), 0, 0);
		return dics.size()>0?dics.get(0):null;
	}
	
	@Override
	public Dictionary getDictionaryByHQl(Map<String,String> prams)throws Exception {
		Dictionary dics = (Dictionary) dictionarydao.getCommonDaoService().getByHQL(this.getHql(prams),null);
		return dics;
	}
	@Override
	public List<Dictionary> getDictionaries(String typecode) {
		Map<String, String> prams=new HashMap<String, String>();
		prams.put("type", typecode);
		List<Dictionary> list=null;
		try {
			 list=dictionarydao.queryDataList(this.getHql(prams), 0, 0);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public List<Dictionary> getParentAreas() {
		String hql = " select distinct t from " + Dictionary.class.getName() + " t," + Program.class.getName() + " a  where t.id=a.area.id or t.parent.id=a.area.id order by t.orderCode asc";
		List<Dictionary> dictionaries = dictionarydao.getCommonDaoService().getListByHQL(hql);
		List<Dictionary> list = new ArrayList<Dictionary>();
		if(null != dictionaries && dictionaries.size() > 0){
			for(Dictionary dictionary : dictionaries){
				if(null != dictionary.getParent()){
					if(!list.contains(dictionary.getParent())){
						list.add(dictionary.getParent());
					}
				}else {
					if(!list.contains(dictionary)){
						list.add(dictionary);
					}
				}
			}
		}
		return list;
	}
	
	@Override
	public List<Map<String, Object>> simpleSelect(Map<String, String> querywhere) {
		try {
			String hql = this.getHql(querywhere);     
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as text ");
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	
	@Override
	public List<Map<String, Object>> simpleSelectScore(Map<String, String> querywhere) {
		try {
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as text ");
			gridsel.put("displayCount", ""+entity_alias_name+".displayCount as displayCount ");
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	
}
